<?php
/**
 * NfxCommissionConfig.php
 *
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace addons\Nsfx\data\service;

use addons\Nsfx\data\model\NfxCommissionDistributionModel;
use addons\Nsfx\data\model\NfxCommissionRegionAgentModel;
use addons\Nsfx\data\model\NfxPromoterModel;

use data\service\BaseService;


/**
 * 商品佣金查询服务层
 */
class NfxCommissionQuery extends BaseService
{
    /**
     * 获取分销佣金 红旗手分享奖励
     */
    public function getCommissionDistributionRecords($params, $page_index, $page_size)
    {
        $uid = $params['uid'];
        $start_time = isset($params['start_time'])? $params['start_time'] : '';
        $end_time = isset($params['end_time'])? $params['end_time'] : '';
        $is_issue = isset($params['is_issue'])? $params['is_issue'] : '0,1,2';

		//获取推广员id
		$promoter_model = new NfxPromoterModel();
		$promoter_info = $promoter_model->getInfo(['uid' => $uid], 'promoter_id');
		$promoter_id = $promoter_info['promoter_id'];

        $alias = 'c';
        $join = [
            ['ns_order o', 'c.order_id = o.order_id', 'left'],
            ['ns_order_goods og', 'c.order_goods_id = og.order_goods_id', 'left'],
        ];
        $field_arr = [
            "c.goods_money, c.goods_return, c.commission_money, c.is_issue, c.create_time",
            "o.user_name, o.order_no, o.create_time",
            "og.goods_name"
        ];
        $order = "o.create_time desc";
        $field = join(',', $field_arr);

        $condition = [
            'c.promoter_id' => $promoter_id,
            'is_issue' => ['in', $is_issue]
        ];
        //发生时间
        if($start_time != '' && $end_time != ''){
            $condition['o.create_time'] = ['between', [strtotime($start_time), strtotime($end_time)]];
        }else if($start_time != '' && $end_time == ''){
            $condition['o.create_time'] = ['egt', strtotime($start_time)];
        }else if($start_time == '' && $end_time != ''){
            $condition['o.create_time'] = ['elt', strtotime($end_time)];
        }

        $commission_model = new NfxCommissionDistributionModel();
        $res = $commission_model->pageQuery($page_index, $page_size, $condition, $order, $field, $alias, $join);
        return $res;
    }

    /**
     * 获取区域佣金 服务中心服务奖励
     */
    public function getCommissionRegionAgentRecords($params, $page_index, $page_size)
    {
        $uid = $params['uid'];
        $start_time = isset($params['start_time'])? $params['start_time'] : '';
        $end_time = isset($params['end_time'])? $params['end_time'] : '';
        $is_issue = isset($params['is_issue'])? $params['is_issue'] : '0,1,2';

        $alias = 'c';
        $join = [
            ['ns_order o', 'c.order_id = o.order_id', 'left'],
            ['ns_order_goods og', 'c.order_goods_id = og.order_goods_id', 'left'],
        ];
        $field_arr = [
            "c.goods_money, c.goods_return, c.commission as commission_money, c.is_issue, c.create_time",
            "o.user_name, o.order_no, o.create_time",
            "og.goods_name"
        ];
        $order = "o.create_time desc";
        $field = join(',', $field_arr);

        $condition = [
            'c.uid' => $uid,
            'is_issue' => ['in', $is_issue]
        ];
        //发生时间
        if($start_time != '' && $end_time != ''){
            $condition['o.create_time'] = ['between', [strtotime($start_time), strtotime($end_time)]];
        }else if($start_time != '' && $end_time == ''){
            $condition['o.create_time'] = ['egt', strtotime($start_time)];
        }else if($start_time == '' && $end_time != ''){
            $condition['o.create_time'] = ['elt', strtotime($end_time)];
        }

        $commission_model = new NfxCommissionRegionAgentModel();
        $res = $commission_model->pageQuery($page_index, $page_size, $condition, $order, $field, $alias, $join);
        return $res;
    }

    /**
     * 获取建立总额
     * @param $uid
     * @param int $confirm_status
     */
    public function getCommissionSum($uid, $confirm_status = null)
    {
        $commission_distribution_model = new NfxCommissionDistributionModel();
        $commission_region_agent_model = new NfxCommissionRegionAgentModel();
        $promoter_model = new NfxPromoterModel();
        $promoter_info = $promoter_model->getInfo(['uid' => $uid]);

        $condition = [
            'promoter_id' => $promoter_info['promoter_id'],
        ];
        if($confirm_status == 0){
            $condition['is_issue'] = ['in', '0,1'];
        }
        if($confirm_status == 1){
            $condition['is_issue'] = ['in', '2'];
        }

        //分销佣金统计
        $commission_distribution_sum = $commission_distribution_model->getSum($condition, 'commission_money');
        //区域佣金统计
        $commission_region_agent_sum = $commission_region_agent_model->getSum($condition, 'commission');

        return round($commission_distribution_sum + $commission_region_agent_sum, 2);
    }
}